﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1.Algorithm
{
    public class ScrewMatrix    //螺旋矩阵
    {
        /* e.g.
            1  8  7
            2  9  6
            3  4  5
        */
        //核心思想：将问题抽象成4个方向的结构化模块
        public void ScrewMatrixFunc()
        {
            BuildScrewMatrix(5);
        }

        private void BuildScrewMatrix(int n)
        {
            int curRow, curCol, maxRow, maxCol, minRow, minCol,matrixValue;
            int[,] screwMatrix = new int[n, n];
            
            curRow = curCol = minRow = minCol = 0;
            maxRow = maxCol = n - 1;    //最大行列索引为阶数-1
            matrixValue=1;
            string direction = "down";

            while(matrixValue <= n * n)
            {
                switch (direction)
                { 
                    case "down":
                        //A[minRow,minCol] -> A[maxRow,minCol]; minCol++;
                        curRow = minRow;
                        curCol = minCol;
                        while (curRow <= maxRow)
                        {
                            screwMatrix[curRow, curCol] = matrixValue++;
                            curRow++;
                        };
                        minCol++;

                        direction = "right";
                        break;
                    case "right":
                        //A[maxRow,minCol] - > A[maxRow,maxCol]; maxRow--;
                        curRow = maxRow;
                        curCol = minCol;
                        while (curCol <= maxCol)
                        {
                            screwMatrix[curRow, curCol] = matrixValue++;
                            curCol++;
                        }
                        maxRow--;

                        direction = "up";
                        break;
                    case "up":
                        //A[maxRow,maxCol] - > A[minRow,maxCol]; maxCol--;
                        curRow = maxRow;
                        curCol = maxCol;
                        while (curRow >= minRow)
                        {
                            screwMatrix[curRow, curCol] = matrixValue++;
                            curRow--;
                        }
                        maxCol--;

                        direction = "left";
                        break;
                    case "left":
                        //A[minRow,maxCol] - > A[minRow,minCol]; minRow++;
                        curRow = minRow;
                        curCol = maxCol;
                        while (curCol >= minCol)
                        {
                            screwMatrix[curRow, curCol] = matrixValue++;
                            curCol--;
                        }
                        minRow++;

                        direction = "down";
                        break;
                }
            }

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write(screwMatrix[i,j]+"\t");
                }
                Console.WriteLine();
            }

        }

    }
}
